//
//  CourseCenterViewController.m
//  xpkc
//
//  Created by TopSageOSX on 14/12/19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "CourseCenterViewController.h"

@interface CourseCenterViewController ()

@property (nonatomic, strong) UISearchController *searchController;

@end

@implementation CourseCenterViewController{
    NSMutableArray *dataArray;
    CourseCenterView *courseview;
   

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"选课中心";
        self.tabBarItem.title = @"选课中心";
        self.tabBarItem.image =[UIImage imageNamed:@"tab_shop"];
        if (IsIOS7) {
            self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_shop_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"选课中心";
   
    [self creatCoursesView];
    [self getCourseData];
  
}

- (void)creatNavigationItem{
    [super creatNavigationItem];
    self.navigationItem.leftBarButtonItem = nil;
}
#pragma mark - creatview
/**
 *  创建视图
 */
- (void)creatCoursesView{
    courseview = [[CourseCenterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 -44)];
    courseview.delegate = self;
    [self.view addSubview:courseview];
}



#pragma mark -
#pragma mark == func ==
/**
 *  获取课程中心数据
 *  大类与子类
 */
- (void)getCourseData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    
    dataArray = [[NSMutableArray alloc]init];
    //获取数据
    NSString *module = @"courseBuy";
    NSString *interface = @"coursecolumn";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString};
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSArray *infoArray = [dic objectForKey:@"info"];
//        logdebug(@"%@",dic);
        [dataArray removeAllObjects];
        NSMutableArray *reids = [NSMutableArray array];
        for (NSDictionary *Dic in infoArray) {
            NSString *reid = [NSString stringWithFormat:@"%@",[Dic valueForKey:@"reid"]];
            BOOL had = NO;
            for (NSString *hadreid in reids) {
                if ([hadreid isEqualToString:reid]) {
                    had = YES;
                }
            }
            if (had) {
             
            }else{
                [reids addObject:reid];
            }
        }
        for (NSString *reid in reids) {
            NSMutableArray *deeparray = [NSMutableArray array];
            NSString *typename = @"";
            for (NSDictionary *dic in infoArray) {
                if ([reid isEqualToString:[NSString stringWithFormat:@"%@",[dic valueForKey:@"reid"]]]) {
                    if ([reid isEqualToString:@"8"]) {
                        break ;
                    }
                    [deeparray addObject:dic];
                }
                if ([reid isEqualToString:[NSString stringWithFormat:@"%@",[dic valueForKey:@"id"]]]) {
                    typename = [dic valueForKey:@"typename"];
                }
            }
            NSDictionary *coursedic = [NSDictionary dictionaryWithObjectsAndKeys:reid,@"id",typename,@"title",deeparray,@"deeparray", nil];
            [dataArray addObject:coursedic];
        }
        courseview.courseArray = dataArray;
        
        /**
         *  判断发送请求的格式是否正确，不正确的话返回的参数是比较蛋疼的
         */
        if ([dic objectForKey:@"timerror"]) {
            logdebug(@"faterror:退出登录");
            [UserMessage sharedUserMessage].isLogin = NO;
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请确保手机时间与北京时间相差五分钟以内" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            return ;
        }
         [hud hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        logdebug(@"%@",error.localizedDescription);
        [hud hide:YES];
    }];
}
/**
 *  delegate
 *
 *  @param btn ann
 */
- (void)courseCenterViewBtnClick:(UIButton *)btn{
    logdebug(@"ID:%ld==%@",btn.tag%10000,btn.titleLabel.text);
    CourseCenterListViewController *courselistVC = [[CourseCenterListViewController alloc]init];
    courselistVC.title = btn.titleLabel.text;
    courselistVC.coursesId = btn.tag%10000;
    courselistVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:courselistVC animated:YES];
}

#pragma mark - == other ==
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
